package com.ruoyi.enterprise.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.enterprise.service.IEContractService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.e_enterprise.EContractRecord;
import com.ruoyi.enterprise.service.IEContractRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同签订记录Controller
 * 
 * @author JYW
 * @date 2024-01-30
 */
@RestController
@RequestMapping("/enterprise/record")
public class EContractRecordController extends BaseController
{
    @Autowired
    private IEContractRecordService eContractRecordService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IEContractService contractService;

    /**
     * 查询合同签订记录列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(EContractRecord eContractRecord)
    {
        startPage();
        List<EContractRecord> list = eContractRecordService.selectEContractRecordList(eContractRecord);
       if (!list.isEmpty()){
           List<EContractRecord> collect = list.stream().peek(contractRecord -> {
               contractRecord.setMayOne(sysUserService.selectUserById(contractRecord.getuId()).getUserName());
               contractRecord.setMayTwo(contractService.selectEContractById(contractRecord.getcId()).getName());
           }).collect(Collectors.toList());
           return getDataTable(collect);
       }
       return getDataTable(list);
    }

    /**
     * 导出合同签订记录列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:record:export')")
    @Log(title = "合同签订记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EContractRecord eContractRecord)
    {
        List<EContractRecord> list = eContractRecordService.selectEContractRecordList(eContractRecord);
        ExcelUtil<EContractRecord> util = new ExcelUtil<EContractRecord>(EContractRecord.class);
        util.exportExcel(response, list, "合同签订记录数据");
    }

    /**
     * 获取合同签订记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(eContractRecordService.selectEContractRecordById(id));
    }

    /**
     * 新增合同签订记录
     */
    @PreAuthorize("@ss.hasPermi('enterprise:record:add')")
    @Log(title = "合同签订记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EContractRecord eContractRecord)
    {
        return toAjax(eContractRecordService.insertEContractRecord(eContractRecord));
    }

    /**
     * 修改合同签订记录
     */
    @PreAuthorize("@ss.hasPermi('enterprise:record:edit')")
    @Log(title = "合同签订记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EContractRecord eContractRecord)
    {
        return toAjax(eContractRecordService.updateEContractRecord(eContractRecord));
    }

    /**
     * 删除合同签订记录
     */
    @PreAuthorize("@ss.hasPermi('enterprise:record:remove')")
    @Log(title = "合同签订记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(eContractRecordService.deleteEContractRecordByIds(ids));
    }
}
